package com.springboot.controller.blog;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.config.controller.BaseController;
import com.springboot.file.BaseFileStoreFileUtil;
import com.springboot.returndata.dto.blog.BlogSortAddUpDto;
import com.springboot.returndata.dto.blog.BlogSortPageDto;
import com.springboot.returndata.vo.blog.BlogSortVo;
import com.springboot.service.blog.BlogSortService;
import com.springboot.util.enums.RespErrorCode;
import com.springboot.util.http.ResponseBuilder;
import com.springboot.util.http.ResultResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 博客分类 前端控制器
 * </p>
 *
 * @author zyj
 * @since 2023-03-23
 */
@RestController
@RequestMapping("/blogSort")
@Slf4j
@Tag(name = "博客管理分类")
public class BlogSortController extends BaseController {

    @Resource
    private BlogSortService blogSortService;

    @Resource
    private BaseFileStoreFileUtil baseFileStoreFileUtil;


    @PostMapping(value = "/pageBlogSort")
    @Operation(description = "获取文章分类列表")
    public ResultResponse<Page<BlogSortVo>> pageBlogSort(@RequestBody BlogSortPageDto blogSortPageDto){
        try {
            blogSortPageDto.setPreViewImg(baseFileStoreFileUtil.getDomain());
            Page<BlogSortVo> blogSort = blogSortService.pageBlogSort(blogSortPageDto);
            return ResponseBuilder.successData(blogSort);
        }catch (Exception e){
            log.error("pageBlogSort error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @GetMapping(value = "/listBlogSort")
    @Operation(description = "获取文章分类所有列表")
    public ResultResponse<BlogSortVo> listBlogSortAll(){
        try {
            List<BlogSortVo> blogSort = blogSortService.listBlogSortAll();
            return ResponseBuilder.successData(blogSort);
        }catch (Exception e){
            log.error("listBlogSortAll error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @PostMapping(value = "/addBlogSort",consumes = "multipart/form-data")
    @Operation(description = "添加文章分类")
    public ResultResponse<String> addBlogSort(BlogSortAddUpDto blogSortAddUpDto,@RequestParam("file") MultipartFile file){

        try {
            blogSortService.addBlogSort(blogSortAddUpDto, file);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("addBlogSort error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }


    @PostMapping(value ="/updateBlogSort",consumes = "multipart/form-data")
    @Operation(description = "修改文章分类")
    public ResultResponse<String> updateBlogSort(BlogSortAddUpDto blogSortAddUpDto,@RequestParam("file") MultipartFile file){
        try {
            blogSortService.updateBlogSort(blogSortAddUpDto, file);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateBlogSort error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }

    @DeleteMapping("/deleteBlogSort")
    @Operation(description = "根据id删除文章分类")
    public ResultResponse<String> deleteBlogSort(@RequestBody Map<String, Integer> param){
        try {
            blogSortService.deleteBlogSort(param.get("id"));
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateBlogSort error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.DELETE_ERROR.getValue(), RespErrorCode.DELETE_ERROR.getDescripe());

    }
}

